package com.nature.yanglegeyangultimate.domain;

import javax.swing.*;
import java.awt.*;

public class Card extends JPanel {

    //给卡牌设置一个id属性,相同的卡牌使用同一个cardId
    int cardId;

    //补充一个index属性，用来记录卡牌在牌堆中的位置
    int index;

    //设置卡牌的坐标，只需要确定左上右下即可
    int xLeft;
    int yUp;
    int xRight;
    int yDown;

    //设置卡牌所在的层数
    int level;

    //设置卡牌是否可以被点击
    boolean canClick = false;

    //设置卡牌的宽高做成默认属性
    private static final int CARD_WIDTH = 80;
    private static final int CARD_HEIGHT = 100;

    Image image;

    //带一个空参构造用于占位
    public Card() {
        //设置背景透明
        this.setOpaque(false);
    }

    /**
     * 仅用于牌堆的原始初始化
     *
     * @param imagePath 图片的路径属性
     */
    public Card(int cardId, String imagePath) {
        this.cardId = cardId;
        image = new ImageIcon(imagePath).getImage();
        //设置背景透明
        this.setOpaque(false);
    }

    /**
     * 用于设置卡牌的坐标的图片渲染路径
     *
     * @param x         横坐标
     * @param y         纵坐标
     * @param imagePath 图片渲染路径
     */
    public Card(int x, int y, String imagePath) {
        //记录卡牌坐标
        this.xLeft = x;
        this.yUp = y;
        this.setBounds(x, y, CARD_WIDTH, CARD_HEIGHT);
        image = new ImageIcon(imagePath).getImage();
        //设置背景透明
        this.setOpaque(false);
    }

    public Card(int cardId, int index, int xLeft, int yUp, int xRight, int yDown, int level, boolean canClick, int CARD_WIDTH, int CARD_HEIGHT, Image image) {
        this.cardId = cardId;
        this.index = index;
        this.xLeft = xLeft;
        this.yUp = yUp;
        this.xRight = xRight;
        this.yDown = yDown;
        this.level = level;
        this.canClick = canClick;
        this.image = image;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            // 获取面板的宽度和高度
            int panelWidth = getWidth();
            int panelHeight = getHeight();

            // 绘制白色圆角矩形
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(Color.WHITE);
            int arcSize = 20; // 圆角的弧度大小
            g2d.fillRoundRect(0, 0, panelWidth, panelHeight, arcSize, arcSize);

            // 按照面板的大小缩放图片并绘制
            g2d.drawImage(image, 0, 0, panelWidth, panelHeight, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (image == null) return super.getPreferredSize();
        return new Dimension(image.getWidth(this), image.getHeight(this));
    }

    public String toString() {
//        return "Card{cardId = " + cardId + ", index = " + index + ", xLeft = " + xLeft + ", yUp = " + yUp + ", xRight = " + xRight + ", yDown = " + yDown + ", level = " + level + ", canClick = " + canClick + ", CARD_WIDTH = " + CARD_WIDTH + ", CARD_HEIGHT = " + CARD_HEIGHT + ", image = " + image + "}";
        return "Card{cardId = " + cardId + ",x=" + xLeft + ",y=" + yUp + ",image = " + image + ",level=" + level;
    }


    /**
     * 获取
     *
     * @return cardId
     */
    public int getCardId() {
        return cardId;
    }

    /**
     * 设置
     *
     * @param cardId
     */
    public void setCardId(int cardId) {
        this.cardId = cardId;
    }

    /**
     * 获取
     *
     * @return index
     */
    public int getIndex() {
        return index;
    }

    /**
     * 设置
     *
     * @param index
     */
    public void setIndex(int index) {
        this.index = index;
    }

    /**
     * 获取
     *
     * @return xLeft
     */
    public int getXLeft() {
        return xLeft;
    }

    /**
     * 设置
     *
     * @param xLeft
     */
    public void setXLeft(int xLeft) {
        this.xLeft = xLeft;
    }

    /**
     * 获取
     *
     * @return yUp
     */
    public int getYUp() {
        return yUp;
    }

    /**
     * 设置
     *
     * @param yUp
     */
    public void setYUp(int yUp) {
        this.yUp = yUp;
    }

    /**
     * 获取
     *
     * @return xRight
     */
    public int getXRight() {
        return xRight;
    }

    /**
     * 设置
     *
     * @param xRight
     */
    public void setXRight(int xRight) {
        this.xRight = xRight;
    }

    /**
     * 获取
     *
     * @return yDown
     */
    public int getYDown() {
        return yDown;
    }

    /**
     * 设置
     *
     * @param yDown
     */
    public void setYDown(int yDown) {
        this.yDown = yDown;
    }

    /**
     * 获取
     *
     * @return level
     */
    public int getLevel() {
        return level;
    }

    /**
     * 设置
     *
     * @param level
     */
    public void setLevel(int level) {
        this.level = level;
    }

    /**
     * 获取
     *
     * @return canClick
     */
    public boolean isCanClick() {
        return canClick;
    }

    /**
     * 设置
     *
     * @param canClick
     */
    public void setCanClick(boolean canClick) {
        this.canClick = canClick;
    }

    /**
     * 获取
     *
     * @return image
     */
    public Image getImage() {
        return image;
    }

    /**
     * 设置
     *
     * @param image
     */
    public void setImage(Image image) {
        this.image = image;
    }
}
